//
// Created by mabinjie on 2024/10/28.
//

#ifndef MESSAGE_FACTORY_HPP
#define MESSAGE_FACTORY_HPP


#include "protocol/message/bodies_pch.hpp"
#include "protocol/message/partial_body.hpp"

namespace protocol {

class MessageFactory {
public:
    static jtt808_body_ptr create(const jtt808_header &header) {
        if (header.attribute_.is_segment) return std::make_shared<PartialBody>(header.attribute_.message_length);
        switch (auto type = header.message_id_) {
            case 0x0001: {
                return std::make_shared<terminal::CommonResponse>();
            }
            case 0x0002: {
                return std::make_shared<terminal::Heartbeat>();
            }
            case 0x0003: {
                return std::make_shared<terminal::Logout>();
            }
            case 0x0004: {
                return std::make_shared<terminal::ServerTimeReq>();
            }
            case 0x0005: {
                return std::make_shared<terminal::RetransferSegmentation>();
            }
            case 0x0100: {
                return std::make_shared<terminal::Registeration>();
            }
            case 0x0102: {
                return std::make_shared<terminal::Verification>();
            }
            case 0x8001: {
                return std::make_shared<platform::CommonResponse>();
            }
            case 0x8003: {
                return std::make_shared<platform::RetransferSegmentation>();
            }
            case 0x8004: {
                return std::make_shared<platform::ServerTimeRes>();
            }
            case 0x8100: {
                return std::make_shared<platform::RegisterResponse>();
            }
            default: {
                return std::make_shared<jtt808_body>();
            }
        }
    }
    inline static std::unordered_map<uint16_t, uint16_t> response_message_id_map = {
        {0x0002, 0x8001},
        {0x0100, 0x8100},
        {0x0003, 0x8001},
        {0x0102, 0x8001},
        {0x0004, 0x8004}
    };

    inline static std::set<uint16_t> response_with_source_sn{0x8001, 0x8100, 0x0001};

    inline static std::set<uint16_t> messages_with_sn_response{0x0002, 0x0003, 0x0100, 0x0102};
};

}

#endif //MESSAGE_FACTORY_HPP
